$(function() {
	//获取页面数据
	init();
});

function init() {
	var jsonParam = {};
	jsonParam.quoName = "";
	jsonParam.status = "";
	jsonParam.updateDateFrom = "";
	jsonParam.updateDateTo = "";
	jsonParam.sortField = "";
	jsonParam.sortDir = "";
	
	padMarketing.ajaxNormal(
			"/pad_marketing/main/quoSearch.action",
			jsonParam,
			function (data){
				dataArr = data;
				displayData(data);	
			},
			function(data){
				
			},
			true, true, "系统正在查询您名下建议书，请耐心等待"
	);
}
var dataArr = null;
function displayData(data) {
	var tpl_proposal = document.getElementById("tpl_proposal").innerHTML;
	var tpl_proposal_check = document.getElementById("tpl_proposal_check").innerHTML;
	var tpl_proposal_update = document.getElementById("tpl_proposal_update").innerHTML;
	var tpl_proposal_delete = document.getElementById("tpl_proposal_delete").innerHTML;
	var tpl_proposal_pay = document.getElementById("tpl_proposal_pay").innerHTML;
	var tpl_proposal_read = document.getElementById("tpl_proposal_read").innerHTML;
	var tpl_proposal_send = document.getElementById("tpl_proposal_send").innerHTML;
	var proposal_list = [];
//	console.log(data);
	for(var i = 0; i < data.length; i++) {
		var proposal = data[i];
//		console.log(proposal.quoId);
		var status = "<label class='sort sort-success'><i>完成</i></label>";
		var proposal_check = "";
		var proposal_update = "";
		var proposal_delete = "";
		var proposal_pay = "";
		var proposal_read = "";
		var proposal_send = "";
		var proposal_img = "/pad_marketing/html/img/login-bg.jpg";
		if(proposal.status == "F") {
			proposal_pay = tpl_proposal_pay.replace(/\{proposalIndex\}/g, i);
			proposal_read = tpl_proposal_read.replace(/\{proposalIndex\}/g, i);
		} else {
			proposal_check = tpl_proposal_check.replace(/\{proposal\}/g, "data-proposal='" + JSON.stringify(proposal) + "'");
			proposal_update = tpl_proposal_update.replace(/\{proposalIndex\}/g, i);
			proposal_delete = tpl_proposal_delete.replace(/\{proposalIndex\}/g, i);
		}
		if(proposal.status == "P") {
			status = "<label class='sort sort-warning'><i>暂存</i></label>";
		} else if(proposal.status == "C") {
			proposal_pay = tpl_proposal_pay.replace(/\{proposalIndex\}/g, i);
			proposal_send = tpl_proposal_send.replace(/\{proposalIndex\}/g, i);
			status = "<label class='sort sort-primary'><i>已初核</i></label>";
		}
//		console.log(JSON.stringify(proposal));
		if(proposal.faceImg != "") {
			proposal_img = proposal.faceImg;
		}
		var proposal_html = tpl_proposal.replace(/\{status\}/g, status)
										.replace(/\{quoName\}/g, HtmlEncode(proposal.quoName))
										.replace(/\{phName\}/g, HtmlEncode(proposal.phName))
										.replace(/\{updateDate\}/g, HtmlEncode(proposal.updateDate))
										.replace(/\{quoId\}/g, proposal.quoId)
										.replace(/\{proposal-update\}/g, proposal_update)
										.replace(/\{proposal-check\}/g, proposal_check)
										.replace(/\{proposal-delete\}/g, proposal_delete)
										.replace(/\{proposal-pay\}/g, proposal_pay)
										.replace(/\{proposal-read\}/g, proposal_read)
										.replace(/\{proposal-img\}/g, proposal_img)
										.replace(/\{proposal-send\}/g, proposal_send)
										.replace(/\{proposalIndex\}/g, i);
//		console.log(proposal_html);
		proposal_list.push(proposal_html);
	}
	$("#proposal-list").html("");
	$("#proposal-list").html("<div class='blank-1'></div>");
	$("#proposal-list").append(proposal_list.join(''));
	$(".bookshelf").popover();
	$(".bookshelf-list").popover();
	if(myScroll) {
		myScroll.refresh();
	}
}
//点击查看,与修改一致，只在url后带view=true
function viewProposal(index, action){
	if(dataArr&&dataArr[index]){
		sessionStorage.returnUrl = '/pad_marketing/plan/' + action + '.action';
		sessionStorage.quoBase = JSON.stringify(dataArr[index]);
		location.href = '/pad_marketing/plan/proposalNew.action?view=true';
	}else {
		console.log('数据异常'+index);
	}
}
//点击修改
function modProposal(index, action){
	if(dataArr&&dataArr[index]){
		sessionStorage.returnUrl = '/pad_marketing/plan/' + action + '.action';
		sessionStorage.quoBase = JSON.stringify(dataArr[index]);
		location.href = '/pad_marketing/plan/proposalNew.action';
	}else {
		console.log('数据异常'+index);
	}
		
}

function deleteProposals() {
	var proposals = [];
	$("input[name='proposal']:checked").each(function() {
		proposals.push($(this).data('proposal'));
	});
	if(proposals.length == 0) {
		alertMsg("请至少选择一项删除");
		return ;
	}
//	console.log(proposals);
	alertMsg("确定要删除所选建议书?", 1, function() {
//		console.log(proposals);
		padMarketing.ajaxNormal('/pad_marketing/main/quoDelete.action', proposals,
			function(data){
				console.log('success');
				$("input[name='proposal']:checked").each(function() {
					$(this).parent().parent().remove();
				});
				$(".icon-return").click();
			},
			function(data){
				alert('失败');
			}
		);
	});
}

function delteOne(id, target) {
	var proposals = [];
	proposals.push(dataArr[id]);
//	console.log($(target).parent().parent().parent().html());
	alertMsg("确定要删除该建议书?", 1, function() {
//		console.log(proposals);
		padMarketing.ajaxNormal('/pad_marketing/main/quoDelete.action', proposals,
			function(data){
				console.log('success');
				$(target).parent().parent().parent().remove();
			},
			function(data){
				alert('失败');
			}
		);
	});
}

function proposalCopy(id) {
	if(!dataArr || !dataArr[id]) {
		console.log("数据有误");
		return ;
	}
	var jsonParam = {};
	jsonParam.quoId = dataArr[id].quoId;
	jsonParam.sign = dataArr[id].sign;
	padMarketing.ajaxNormal('/pad_marketing/main/quoCopy.action', jsonParam,
		function(data){
			console.log('success');
			init();
		},
		function(data){
			alert('失败');
		}
	);
}

function reset() {
	$("#quoName").val('');
	$("#phName").val('');
	$("#updateDateFrom").val('');
	$("#updateDateTo").val('');
	$("#status").val('');
}

function dateCheck() {
	var updateDateFrom = $("#updateDateFrom").val();
	var updateDateTo = $("#updateDateTo").val();
	if(updateDateFrom && updateDateTo && updateDateFrom > updateDateTo) {
		alertMsg('起止日期不能大于结束日期');
		$("#updateDateFrom").val('');
		$("#updateDateTo").val('');
	}
}

function quoSearch() {
	var jsonParam = {};
	jsonParam.quoId = "";
	jsonParam.quoName = $("#quoName").val();
	jsonParam.phName = $("#phName").val();
	jsonParam.status = $("#status").val();
	jsonParam.updateDateFrom = $("#updateDateFrom").val();
	jsonParam.updateDateTo = $("#updateDateTo").val();
	jsonParam.sortField = "";
	jsonParam.sortDir = "";
	padMarketing.ajaxNormal('/pad_marketing/main/quoSearch.action', jsonParam,
		function(data){
			dataArr = data;
			displayData(data);
			closeDiv('open1');
			reset();
		},
		function(data){
			alertMsg('查询失败请重试');
		},
		true, true, "系统正在查询您名下建议书，请耐心等待"
	);
}

function payProposal(index, action) {
	var quoBase = dataArr[index];
	padMarketing.ajaxNormal('/pad_marketing/main/quoToApps.action', quoBase,
		function(data) {
			var appBases = data;
			sessionStorage.returnUrl = '/pad_marketing/plan/' + action + '.action';
			if(appBases.length == 1) {
				sessionStorage.appBase = JSON.stringify(appBases[0]);
				window.location.href = "/pad_marketing/purchaseNote/index.action";
			}else{
				$('#change').remove();
				var p = '<div class="open-body font-16"  id="change"  ><p  class="add font-12 gray">请点选其中的一张订单继续投保操作，剩余订单请在订单书架中查找</p></div>';
				$('#open6 h4').after(p);
				for (var i = 0; i < appBases.length; i++) {

					var add_risk = '<div class="show"><div class="blank-1"></div><div class="form-group"><div class="col-md-4"><p class="p-overflow" title="">'
							+ appBases[i].productName
							+ '</p></div><div class="col-md-6 text-right"><p class="p-overflow">订单号：<span class="orange">'
							+ appBases[i].orderId
							+ '</span></p> </div><div class="col-md-2 text-right"><div  class="radio_blo"><input id="input'
							+ i
							+ '" data="'
							+ appBases[i]
							+ '" type="radio" name="xx" /><label for="input'
							+ i
							+ '">&nbsp;</label></div></div></div><hr></div>';
					$('.add').append(add_risk);
				}

				$(".radio_blo input").click(function() {
					sessionStorage.removeItem('appBase');
					var ind = $("input[name=xx]:checked").closest('.show').index();
					if(ind!=-1){
						$('#text-center').off().on('click',function(){
							sessionStorage.appBase = JSON.stringify(appBases[ind]);
							window.location.href = "/pad_marketing/purchaseNote/index.action";	
						});
					}
					
				});
				showDiv('open6');
			}
		},
		function(data) {
			alertMsg(data.responseText);
			console.log('失败');
		}
	);
}

function toProposal(index, action) {
	var proposal = dataArr[index];
	sessionStorage.returnUrl = '/pad_marketing/plan/' + action + '.action';
	sessionStorage.quoBase = JSON.stringify(dataArr[index]);
	window.location.href = "/pad_marketing/plan/proposal.action?quoId=" + proposal.quoId + "&sign=" + proposal.sign;
}